package by.semerikov.parser;

import by.semerikov.parts.Log;

public class TextLineParser {
	private CodeLineParser codeLineParser = new CodeLineParser();
	private DateLineParser dateLineParser = new DateLineParser();
	private IpStringParser ipLineParser = new IpStringParser();
	private RequestLineParser requestLineParser = new RequestLineParser();
	private SizeReturnedLineParser sizeReturnedLineParser = new SizeReturnedLineParser();
	private HttpVersionParser httpVersionParser = new HttpVersionParser();
	private TimeOffsetParser timeOffsetParser =  new TimeOffsetParser();
	
	public Log getLog(String line)
	{
		Log result = new Log();
		int begin = 0;
		int i = 0;
		for (; i < line.length(); i++) {
			if(line.charAt(i) == '-')
			{
				result.setIp(ipLineParser.getObject(line.substring(0,i)));
				break;
			}
		}
		for (; i < line.length(); i++) {
			if(line.charAt(i) == '[' && begin == 0)
			{
				begin = i+1;
			}
			if(line.charAt(i) == ' ')
			{
				result.setDate(dateLineParser.getObject(line.substring(begin, i)));
				break;
			}
		}
		begin = 0;
		for (; i < line.length(); i++) {
			if(line.charAt(i) == '+' && begin == 0)
			{
				begin = i+1;
			}
			if(line.charAt(i) == ']' && begin != 0)
			{
				result.setTimeOffset(timeOffsetParser.getObject(line.substring(begin, i)));
				break;
			}
		}
		begin = 0;
		int index = 0;
		for (; i < line.length(); i++) {
			
			if(line.charAt(i) == '"' && begin == 0)
			{
				begin = i+1;
			}
			if(line.charAt(i) == ' ' && begin != 0)
			{
				if(index == 0)
				{
					index++;
				}
				else
				{
					result.setRequestLine(requestLineParser.getObject(line.substring(begin, i)));
					break;
				}
			}
		}
		begin = i;
		index = 0;
		for (; i < line.length(); i++) {
			if(line.charAt(i) == '"' )
			{
					result.setHttpVersion(httpVersionParser.getObject(line.substring(begin, i)));
					break;
			}
		}
		begin = 0;
		for (; i < line.length(); i++) {
			
			if(line.charAt(i) == ' ' && begin == 0)
			{
				begin = i+1;
			}
			if(line.charAt(i) == ' ' && begin != 0 && begin != i+1)
			{
				result.setCode(codeLineParser.getObject(line.substring(begin, i)));
				i--;
				break;
			}
		}
		begin = 0;
		for (; i < line.length(); i++) {
			
			if(line.charAt(i) == ' ' && begin == 0)
			{
				begin = i+1;
				result.setSizeReturned(sizeReturnedLineParser.getObject(line.substring(begin )));
				break;
			}
			
				
				
			
		}
		return result;
	}
}
